#include "cap_gt9xx.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>


#ifdef GT9xx_Soft_I2C
/**********************驱动初始化配置*********************/
static WHT_Soft_I2C_Cache_t wht_cache;

static void WHT_AT24Cxx_Driver_Init(void)
{
    if (WHT_Soft_I2C_BUS1_Init() != 0)
    {
        /* 总线初始化失败 */
        return;
    }
    wht_cache.Cmd_Count = 1;
}
/**********************驱动初始化配置*********************/

/************************** start read & write *******************************/
static void WHT_AT24Cxx_Driver_Write(uint8_t addr_7bit, uint8_t* const input_buf, uint32_t count)
{
    while (WHT_Soft_I2C_BUS1->Mutex == I2C_Lock)//等待互斥锁成功打开
        vTaskDelay(1);
    wht_cache.Addr_7Bit = addr_7bit;
    wht_cache.Dir = Soft_I2C_Write;
    //wht_cache.Cmd[0] = cmd;
    wht_cache.Buffer = input_buf;
    wht_cache.Buffer_Count = count;

    WHT_Soft_I2C_BUS_OPS.Read_Write(WHT_Soft_I2C_BUS1, &wht_cache);
}
static void WHT_AT24Cxx_Driver_Read(uint8_t addr_7bit, uint8_t* const output_buf, uint32_t count)
{
    while (WHT_Soft_I2C_BUS1->Mutex == I2C_Lock)//等待互斥锁成功打开
        vTaskDelay(1);
    wht_cache.Addr_7Bit = addr_7bit;
    wht_cache.Dir = Soft_I2C_Read;
    //wht_cache.Cmd[0] = cmd;
    wht_cache.Buffer = output_buf;
    wht_cache.Buffer_Count = count;

    WHT_Soft_I2C_BUS_OPS.Read_Write(WHT_Soft_I2C_BUS1, &wht_cache);
}
/************************** end read & write ********************************/
#else
/**********************驱动初始化配置*********************/
static SemaphoreHandle_t Tx_xSemaphore;//二值信号量句柄
static SemaphoreHandle_t Rx_xSemaphore;
static SemaphoreHandle_t Exti_xSemaphore;

static WHT_I2C_Cache_t wht_cache;
static WHT_EXTI_Config_t wht_config;

static void WHT_Tx_Idle_IT_Callback(void);
static void WHT_Rx_Idle_IT_Callback(void);
static void WHT_Exti_IT_Callback(void);
static void WHT_GT9xx_Reset(void);
static void App_Touch_Task(void *parameter);
static void WHT_GT9xx_Driver_Init(void)
{
	/* I2C初始化 */
    if (WHT_I2C_BUS2_Init() != 0)
    {
        /* 总线初始化失败 */
        return;
    }
    wht_cache.Cmd_Count = 2;
    wht_cache.Read_Finish_IT_CB = WHT_Rx_Idle_IT_Callback;
    wht_cache.Write_Finish_IT_CB = WHT_Tx_Idle_IT_Callback;

    Tx_xSemaphore = xSemaphoreCreateBinary();
    Rx_xSemaphore = xSemaphoreCreateBinary();

	/* 辅助引脚初始化 */
    WHT_GPIO_BSP.WHT_Set_Clock(GPT9xx_RST_Port, ENABLE);
    WHT_GPIO_BSP.WHT_Set_Pin(GPT9xx_RST_Port, GPT9xx_RST_Pin, Low);
	WHT_GPIO_BSP.WHT_Set_Mode(GPT9xx_RST_Port, GPT9xx_RST_Pin, Mode_OPD_PP);

	WHT_GPIO_BSP.WHT_Set_Clock(GPT9xx_INT_Port, ENABLE);
	WHT_GPIO_BSP.WHT_Set_Pin(GPT9xx_INT_Port, GPT9xx_INT_Pin, Low);
	WHT_GPIO_BSP.WHT_Set_Mode(GPT9xx_INT_Port, GPT9xx_INT_Pin, Mode_OPD_PP);

	WHT_GT9xx_Reset();
	Exti_xSemaphore = xSemaphoreCreateBinary();

	wht_config.Type = Exti_GPIO;
	wht_config.Line = GPT9xx_INT_EXTI_Line;
	wht_config.Trigger = Trigger_Rising;
	wht_config.Mode = Mode_Interrupt;
	wht_config.Interrupt_Callback = WHT_Exti_IT_Callback;
	wht_config.Portx = GPT9xx_INT_Port;
	wht_config.Pinx = GPT9xx_INT_Pin;
	WHT_EXTI_BSP.WHT_Register(&wht_config);
	WHT_EXTI_BSP.WHT_Config(&wht_config);

	xTaskCreate(App_Touch_Task, "App_Touch_Task", 200, NULL, configMAX_PRIORITIES / 2, NULL);
}
/**********************驱动初始化配置*********************/


/************************** start read & write *******************************/
/* 发送空闲中断回调函数 */
static void WHT_Tx_Idle_IT_Callback(void)
{
    BaseType_t xHigherPriorityTaskWoken;

    xSemaphoreGiveFromISR(Tx_xSemaphore, &xHigherPriorityTaskWoken);//释放信号量
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
/* 接收空闲中断回调函数 */
static void WHT_Rx_Idle_IT_Callback(void)
{
    BaseType_t xHigherPriorityTaskWoken;

    xSemaphoreGiveFromISR(Rx_xSemaphore, &xHigherPriorityTaskWoken);//释放信号量
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
/* 触摸中断中断回调函数 */
static void WHT_Exti_IT_Callback(void)
{
    BaseType_t xHigherPriorityTaskWoken;

	// 这里要把中断关闭掉 测到触控未按下在去开中断交给中断检测
	WHT_GT9xx_Driver.WHT_IRQ_Disable();
    xSemaphoreGiveFromISR(Exti_xSemaphore, &xHigherPriorityTaskWoken);//释放信号量
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

static bool WHT_GT9xx_Driver_Write(uint8_t addr_7bit, uint16_t cmd, uint8_t* const input_buf, uint32_t count)
{
    while (WHT_I2C_BUS2->Mutex == I2C_Lock)//等待互斥锁成功打开
        vTaskDelay(1);
    wht_cache.Addr_7Bit = addr_7bit;
    wht_cache.Dir = I2C_Write;
	wht_cache.Cmd[0] = cmd >> 8;
	wht_cache.Cmd[1] = cmd & 0xff;
    wht_cache.Buffer = input_buf;
    wht_cache.Buffer_Count = count;

    TickType_t Tick_Count = count * 9 * 1000  / WHT_I2C_BUS2->Config.I2C_Rate_Hz / portTICK_RATE_MS;//单位ms/Tick
    if (Tick_Count)
        WHT_I2C_BUS_OPS.Read_Write_IT(WHT_I2C_BUS2, &wht_cache);
    else
        WHT_I2C_BUS_OPS.Read_Write(WHT_I2C_BUS2, &wht_cache);
    
    if (WHT_I2C_BUS2->Mutex == I2C_Unlock)//传输结束
    {
        /*用户可以加入判断是否有错误相关代码*/
    }
    else
        xSemaphoreTake(Tx_xSemaphore, portMAX_DELAY);//等待信号量
	return WHT_I2C_BUS2->State == I2C_No_Error ? true : false;
}
static bool WHT_GT9xx_Driver_Read(uint8_t addr_7bit, uint16_t cmd, uint8_t* const output_buf, uint32_t count)
{
    while (WHT_I2C_BUS2->Mutex == I2C_Lock)//等待互斥锁成功打开
        vTaskDelay(1);
    wht_cache.Addr_7Bit = addr_7bit;
    wht_cache.Dir = I2C_Read;
	wht_cache.Cmd[0] = cmd >> 8;
	wht_cache.Cmd[1] = cmd & 0xff;
    wht_cache.Buffer = output_buf;
    wht_cache.Buffer_Count = count;

    TickType_t Tick_Count = count * 9 * 1000  / WHT_I2C_BUS2->Config.I2C_Rate_Hz / portTICK_RATE_MS;//单位ms/Tick
    if (Tick_Count)
        WHT_I2C_BUS_OPS.Read_Write_IT(WHT_I2C_BUS2, &wht_cache);
    else
        WHT_I2C_BUS_OPS.Read_Write(WHT_I2C_BUS2, &wht_cache);
    
    if (WHT_I2C_BUS2->Mutex == I2C_Unlock)//传输结束
    {
        /*用户可以加入判断是否有错误相关代码*/
    }
    else
        xSemaphoreTake(Rx_xSemaphore, portMAX_DELAY);//等待信号量
	return WHT_I2C_BUS2->State == I2C_No_Error ? true : false;
}
/************************** end read & write ********************************/
#endif /*GT9xx_Soft_I2C*/

/* 探测总线模块个数 */
static uint8_t WHT_BUS_Scan_I2C_Slave_Count(void)
{
#ifdef GT9xx_Soft_I2C
    return WHT_Soft_I2C_BUS_OPS.Scan_Mount_Count(WHT_Soft_I2C_BUS1);
#else
    return WHT_I2C_BUS_OPS.Scan_Mount_Count(WHT_I2C_BUS2);
#endif /* GT9xx_Soft_I2C */
}
/* 复位 */
static void WHT_GT9xx_Reset(void)
{
	/* INT重置下拉推挽输出 */
	WHT_GPIO_BSP.WHT_Set_Pin(GPT9xx_INT_Port, GPT9xx_INT_Pin, Low);
	WHT_GPIO_BSP.WHT_Set_Mode(GPT9xx_INT_Port, GPT9xx_INT_Pin, Mode_OPD_PP);

	/* RST低电平 */
	WHT_GPIO_BSP.WHT_Set_Pin(GPT9xx_RST_Port, GPT9xx_RST_Pin, Low);
	vTaskDelay(20 / portTICK_PERIOD_MS);
	/* RST高电平 */
	WHT_GPIO_BSP.WHT_Set_Pin(GPT9xx_RST_Port, GPT9xx_RST_Pin, Hig);
	vTaskDelay(20 / portTICK_PERIOD_MS);

	/* INT重置浮空输入 开始接收中断信号 */
	WHT_GPIO_BSP.WHT_Set_Mode(GPT9xx_INT_Port, GPT9xx_INT_Pin, Mode_In_FLOATING);
}
/* 中断使能 */
static void WHT_GT9xx_IRQ_Enable(void)
{
	WHT_EXTI_BSP.WHT_Line_Cmd(&wht_config, ENABLE);//开启此路中断功能
}
/* 中断失能 */
static void WHT_GT9xx_IRQ_Disable(void)
{
	WHT_EXTI_BSP.WHT_Line_Cmd(&wht_config, DISABLE);//关闭此路中断功能
}




/* 下面的属于触摸操作部分 */
/* 5寸屏GT9157配置参数 */
static const uint8_t GT9157_Config_Value[] =
{ 
	0x00,0x20,0x03,0xE0,0x01,0x05,0x3C,0x00,0x01,0x08,
	0x28,0x0C,0x50,0x32,0x03,0x05,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x17,0x19,0x1E,0x14,0x8B,0x2B,0x0D,
	0x33,0x35,0x0C,0x08,0x00,0x00,0x00,0x9A,0x03,0x11,
	0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x32,0x00,0x00,
	0x00,0x20,0x58,0x94,0xC5,0x02,0x00,0x00,0x00,0x04,
	0xB0,0x23,0x00,0x93,0x2B,0x00,0x7B,0x35,0x00,0x69,
	0x41,0x00,0x5B,0x4F,0x00,0x5B,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,
	0x12,0x14,0x16,0x18,0x1A,0xFF,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0F,
	0x10,0x12,0x13,0x16,0x18,0x1C,0x1D,0x1E,0x1F,0x20,
	0x21,0x22,0x24,0x26,0xFF,0xFF,0xFF,0xFF,0x00,0x00,
	0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
	0xFF,0xFF,0xFF,0xFF,0x48,0x01
};

/* 7寸屏GT911配置参数 */
static const uint8_t GT911_Config_Value[] =
{
	0x42,0x00,0x04,0x58,0x02,0x05,0x4C,0x00,0x01,0x08,0x28,0x05,0x50,
	0x32,0x03,0x05,0x00,0x00,0x00,0x00,0x99,0x55,0x00,0x00,0x00,0x00,
	0x00,0x8C,0x2E,0x0E,0x17,0x15,0x31,0x0D,0x00,0x00,0x01,0x9A,0x04,
	0x1D,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x0F,
	0x36,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,0x9C,0x11,0x00,0x7B,0x16,
	0x00,0x63,0x1C,0x00,0x4E,0x25,0x00,0x3F,0x2F,0x00,0x3F,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x1A,0x18,0x16,0x14,
	0x12,0x10,0x0E,0x0C,0x0A,0x08,0x06,0x04,0x02,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x02,0x04,0x06,0x08,0x0A,0x0C,0x0F,0x10,0x12,0x13,0x14,0x16,0x18,
	0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x24,0x26,0x28,0x29,0x2A,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x78,0x01
};

/* 触屏缓存 */
static uint8_t WHT_Touch_TRx_Buffer[GT9xx_Config_Max_Length];
/* 触屏信息 */
static WHT_GT9xx_Info_t WHT_GT9xx_Info;
/* 触摸坐标 */
static WHT_Touch_Pos_t WHT_Touch_Pos[GT9xx_Max_Touch_Count];



/* 读取固件号 */
static void WHT_Touch_Read_Version(void)
{
	if (WHT_GT9xx_Driver_Read(WHT_GT9xx_Info.addr, REG_PID, WHT_Touch_TRx_Buffer, 6) == false)
	{
		printf("WHT_Touch_Read_Version Error\r\n");
		return;
	}

	if (WHT_Touch_TRx_Buffer[3] == 0x00)
    {
        printf("IC1 Version: GT%c%c%c_%02x%02x\r\n", WHT_Touch_TRx_Buffer[0], WHT_Touch_TRx_Buffer[1], WHT_Touch_TRx_Buffer[2], WHT_Touch_TRx_Buffer[5], WHT_Touch_TRx_Buffer[4]);
		//GT911芯片
		if (WHT_Touch_TRx_Buffer[0] == '9' && WHT_Touch_TRx_Buffer[1] == '1' && WHT_Touch_TRx_Buffer[2] == '1')
		{
			WHT_GT9xx_Info.name = Name_GT911;
		}
    }
    else
    {
        printf("IC2 Version: GT%c%c%c%c_%02x%02x\r\n", WHT_Touch_TRx_Buffer[0], WHT_Touch_TRx_Buffer[1], WHT_Touch_TRx_Buffer[2], WHT_Touch_TRx_Buffer[3], WHT_Touch_TRx_Buffer[5], WHT_Touch_TRx_Buffer[4]);
		//GT9157芯片
		if (WHT_Touch_TRx_Buffer[0] == '9' && WHT_Touch_TRx_Buffer[1] == '1' && WHT_Touch_TRx_Buffer[2] == '5' && WHT_Touch_TRx_Buffer[3] == '7')
		{
			WHT_GT9xx_Info.name = Name_GT9157;
		}
	}
}
/* 触屏按下 跟踪ID x坐标 y坐标 触摸大小 */
static void WHT_Touch_Down(uint8_t id, uint16_t x, uint16_t y, uint16_t w)
{
	/* (x,y) 即为最新的触摸点 *************/
	/*id为轨迹编号(多点触控时有多轨迹)*/
	WHT_Touch_Pos[id].New_X = x;
	WHT_Touch_Pos[id].New_Y = y;
	WHT_Touch_Pos[id].New_W = w;
	WHT_Touch_Pos[id].State = true;
	//printf("Touch Down x = %u, y = %u, w = %d\r\n", x, y, w);
}
/* 触屏释放 */
static void GTP_Touch_Up(uint8_t id)
{
	/*在此处添加自己的触摸点释放时的最新的释放点处理*/
	/***id为轨迹编号(多点触控时有多轨迹)********/
	WHT_Touch_Pos[id].State = false;
	WHT_Touch_Pos[id].New_X = 0;
	WHT_Touch_Pos[id].New_Y = 0;
	WHT_Touch_Pos[id].New_W = 0;
}
/* 触屏复位 */
static void WHT_Touch_Reset(void)
{
	/* 硬件复位 */
	////WHT_GT9xx_Driver.WHT_Reset();

	/* 软件复位 */
	WHT_Touch_TRx_Buffer[0] = 2;
	WHT_GT9xx_Driver_Write(WHT_GT9xx_Info.addr, REG_CTRL, WHT_Touch_TRx_Buffer, 1);
	vTaskDelay(20);
	WHT_Touch_TRx_Buffer[0] = 0;
	WHT_GT9xx_Driver_Write(WHT_GT9xx_Info.addr, REG_CTRL, WHT_Touch_TRx_Buffer, 1);
}
/* 设置最大输出坐标 */
static void WHT_Touch_Set_Max_Output_X_Y(uint16_t x, uint16_t y)
{
	WHT_Touch_TRx_Buffer[0] = x & 0xff;
	WHT_Touch_TRx_Buffer[1] = x >> 8;

	WHT_Touch_TRx_Buffer[2] = y & 0xff;
	WHT_Touch_TRx_Buffer[3] = y >> 8;

	if (WHT_GT9xx_Driver_Write(WHT_GT9xx_Info.addr, REG_X_MAX, WHT_Touch_TRx_Buffer, 4) == true)
	{
		WHT_GT9xx_Info.max_x_width = x;
		WHT_GT9xx_Info.max_y_height = y;
	}
}
/* 设置中断触发模式 */
static void WHT_Touch_Set_IT_Trigger_Mode(uint8_t mode)
{
	WHT_GT9xx_Driver_Read(WHT_GT9xx_Info.addr, REG_SWITCH, WHT_Touch_TRx_Buffer, 1);

	WHT_Touch_TRx_Buffer[0] &= ~0x03;
	WHT_Touch_TRx_Buffer[0] |= mode;
	WHT_GT9xx_Driver_Write(WHT_GT9xx_Info.addr, REG_SWITCH, WHT_Touch_TRx_Buffer, 1);
}
/* 触屏处理 */
static bool WHT_Touch_Scan(void)
{
	static struct
	{
		uint8_t track_id;
		uint8_t x_low;
		uint8_t x_high;
		uint8_t y_low;
		uint8_t y_high;
		uint8_t w_low;
		uint8_t w_high;
		uint8_t reserved;
	}Touch_Data_Buffer[GT9xx_Max_Touch_Count] = {0};
	static uint8_t last_touch_number = 0;
	static uint8_t last_track_id[GT9xx_Max_Touch_Count] = {0};
	uint8_t  new_touch_number;
	uint8_t  buffer_status;

	if (WHT_GT9xx_Driver_Read(WHT_GT9xx_Info.addr, REG_GSTID, WHT_Touch_TRx_Buffer, 1) == false)
	{
		return false;
	}
	else
	{
		buffer_status = WHT_Touch_TRx_Buffer[0] >> 7;     // 触摸就绪状态
		new_touch_number = WHT_Touch_TRx_Buffer[0] & 0x0f;// 触摸点数
	}
	
	/* 触摸数据有效状态和触点数 */
	if ((buffer_status == false) || (new_touch_number > GT9xx_Max_Touch_Count))
	{
		WHT_Touch_TRx_Buffer[0] = 0;
		WHT_GT9xx_Driver_Write(WHT_GT9xx_Info.addr, REG_GSTID, WHT_Touch_TRx_Buffer, 1);
		return false;
	}

	/* 获取触点数据 */
    if (new_touch_number > 0)
	{
		WHT_GT9xx_Driver_Read(WHT_GT9xx_Info.addr, REG_TP1, (uint8_t*)Touch_Data_Buffer, new_touch_number * (sizeof(Touch_Data_Buffer) / sizeof(Touch_Data_Buffer[0])));
	}
	
	/* 是否有触点释放 */
    if (last_touch_number > new_touch_number)
	{
		for (size_t i = 0; i < last_touch_number; i++)
		{
			for (size_t j = 0; j < new_touch_number; )
			{
				if (last_track_id[i] == Touch_Data_Buffer[j].track_id)
				{
					break;
				}

				/* 跟踪ID找不到 */
				if (++j >= new_touch_number)
				{
					GTP_Touch_Up(last_track_id[i]);
				}
			}
		}
	}
	
	/* 是否有触点按下 */
	if (new_touch_number)
	{
		for (size_t i = 0; i < new_touch_number; i++)
		{
			last_track_id[i] = Touch_Data_Buffer[i].track_id;
			WHT_Touch_Down(Touch_Data_Buffer[i].track_id, (Touch_Data_Buffer[i].x_high << 8) | Touch_Data_Buffer[i].x_low,
			                                              (Touch_Data_Buffer[i].y_high << 8) | Touch_Data_Buffer[i].y_low,
														  (Touch_Data_Buffer[i].w_high << 8) | Touch_Data_Buffer[i].w_low);
		}
	}
	else if (last_touch_number)
	{
		for (size_t i = 0; i < last_touch_number; i++)
		{
			GTP_Touch_Up(last_track_id[i]);
		}
	}

    last_touch_number = new_touch_number;

	WHT_Touch_TRx_Buffer[0] = 0;
	WHT_GT9xx_Driver_Write(WHT_GT9xx_Info.addr, REG_GSTID, WHT_Touch_TRx_Buffer, 1);
	return true;
}
/* 触屏配置 */
static void WHT_Touch_Config(void)
{
    uint8_t Config_Count = REG_CHECK - REG_CFGS;  //需要配置的寄存器个数
    uint8_t Check_Sum;

	/* 必要的延迟 */
	vTaskDelay(50 / portTICK_PERIOD_MS);

	//根据IC的型号指向不同的配置
	if (WHT_GT9xx_Info.name == Name_GT9157)
	{
		printf("GT9157 Config\r\n");
		memcpy(&WHT_Touch_TRx_Buffer, GT9157_Config_Value, Config_Count);
	}
	else if (WHT_GT9xx_Info.name == Name_GT911)
	{
		printf("GT911 Config\r\n");
		memcpy(&WHT_Touch_TRx_Buffer, GT911_Config_Value, Config_Count);
	}
	else
	{
		printf("No Touch Name\r\n");
		return;
	}
		
    //计算要写入checksum寄存器的值
    Check_Sum = 0;
	for (size_t i = 0; i < Config_Count; i++)
	{
        Check_Sum += WHT_Touch_TRx_Buffer[i];
    }

	/* 校验值 */
	WHT_Touch_TRx_Buffer[Config_Count] = (~Check_Sum) + 1;
	/* 配置保存 */
	WHT_Touch_TRx_Buffer[Config_Count + 1] = 1;

    //写入配置
	if (WHT_GT9xx_Driver_Write(WHT_GT9xx_Info.addr, REG_CFGS, WHT_Touch_TRx_Buffer, Config_Count + 2) == false)
	{
		printf("Config Touch Value Error\r\n");
		return;
	}
	vTaskDelay(50 / portTICK_PERIOD_MS); //延迟等待芯片更新

	memset(WHT_Touch_TRx_Buffer, 0, sizeof(WHT_Touch_TRx_Buffer));
	WHT_GT9xx_Driver_Read(WHT_GT9xx_Info.addr, REG_CFGS, WHT_Touch_TRx_Buffer, Config_Count + 2);
	for (size_t i = 0; i < 6; i++)
	{
		printf("Dump 0x%02x ", WHT_Touch_TRx_Buffer[i]);
	}
	printf("\r\n");
}


/* 触摸任务 */
static void App_Touch_Task(void *parameter)
{
	WHT_GT9xx_Info.addr = GT9xx_7Bit_Addr2;
	WHT_GT9xx_Info.max_x_width = RGB_PIXEL_WIDTH;
	WHT_GT9xx_Info.max_y_height = RGB_PIXEL_HEIGHT;

	memset(WHT_Touch_Pos, 0, sizeof(WHT_Touch_Pos));
	memset(WHT_Touch_TRx_Buffer, 0, sizeof(WHT_Touch_TRx_Buffer));

	//获取触屏型号
    WHT_Touch_Read_Version();
	//配置触屏参数
	WHT_Touch_Config();

	/* 使能中断 */
	WHT_GT9xx_Driver.WHT_IRQ_Enable();

    for (; ;)
    {
        /* 等待信号量 */
		if (xSemaphoreTake(Exti_xSemaphore, portMAX_DELAY) == pdTRUE)
		{
			for (; ;)
			{
				if (WHT_Touch_Scan() == false)
				{
					WHT_GT9xx_Driver.WHT_IRQ_Enable();
					break;
				}
				vTaskDelay(10 / portTICK_PERIOD_MS);	
			}
		}
    }
}


const WHT_GT9xx_Driver_t WHT_GT9xx_Driver =
{
	.WHT_Info                 = &WHT_GT9xx_Info,
	.WHT_Touch_Pos            = (const WHT_Touch_Pos_t (*)[GT9xx_Max_Touch_Count])&WHT_Touch_Pos,
    .WHT_Init                 = WHT_GT9xx_Driver_Init,
    .WHT_Scan_I2C_Slave_Count = WHT_BUS_Scan_I2C_Slave_Count,
	.WHT_Reset                = WHT_GT9xx_Reset,
	.WHT_IRQ_Enable           = WHT_GT9xx_IRQ_Enable,
	.WHT_IRQ_Disable          = WHT_GT9xx_IRQ_Disable,
};
